package com.jellyleo.activiti.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jellyleo.activiti.entity.ActRuTask;
import com.jellyleo.activiti.entity.dto.ActTaskDTO;
import com.jellyleo.activiti.entity.dto.ProcessDefinitionDTO;
import com.jellyleo.activiti.service.ActRuTaskService;
import com.jellyleo.activiti.mapper.ActRuTaskMapper;
import com.woniu.dto.ActRuTaskDto;
import com.woniu.pojo.PageParam;
import com.woniu.until.ResponseData;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ExecutionQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author dell is user
* @description 针对表【ACT_RU_TASK】的数据库操作Service实现
* @createDate 2023-10-31 15:34:55
*/
@Service
public class ActRuTaskServiceImpl extends ServiceImpl<ActRuTaskMapper, ActRuTask>
    implements ActRuTaskService{

    @Autowired
    private TaskService taskService;
    @Autowired
    HistoryService historyService;
    @Autowired
    private RuntimeService runtimeService;
    @Override
    public ResponseData<?> getTaskList(PageParam<ActRuTask> param) {

        TaskQuery taskQuery = taskService.createTaskQuery()
                .taskAssignee(param.getName());
        List<Task> tasks = taskQuery.list();
        List<ActTaskDTO> actTaskDTOList = new ArrayList<>();
        for (Task task : tasks) {
            ActTaskDTO actTaskDTO = new ActTaskDTO();
            actTaskDTO.setId(task.getId());
            String processInstanceId = task.getProcessInstanceId();

            String  businessKey =(String) runtimeService.getVariable(processInstanceId, "businessKey");

            actTaskDTO.setBusinessKey(businessKey);
            actTaskDTO.setName(task.getName());
            actTaskDTO.setStatus(String.valueOf(0));
            actTaskDTO.setCreatedDate(task.getCreateTime());
            actTaskDTO.setAssignee(task.getAssignee());
            actTaskDTOList.add(actTaskDTO);
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        PageInfo<ActTaskDTO> actTaskDTOPageInfo = new PageInfo<>(actTaskDTOList);
        return new ResponseData<>().ok(actTaskDTOPageInfo);

    }

    @Override
    public ResponseData<?> getTaskHistoryList(PageParam<ActRuTask> param) {


        HistoricTaskInstanceQuery query = historyService.createHistoricTaskInstanceQuery().taskAssignee(param.getName());
        // 执行查询，获取结果集
        List<HistoricTaskInstance> historicTasks = query.list();
        List<ActTaskDTO> actTaskDTOList = new ArrayList<>();
        for (HistoricTaskInstance task : historicTasks) {
            ActTaskDTO actTaskDTO = new ActTaskDTO();
            actTaskDTO.setId(task.getId());
            actTaskDTO.setName(task.getName());
            actTaskDTO.setStatus(String.valueOf(0));
            actTaskDTO.setCreatedDate(task.getCreateTime());
            actTaskDTO.setAssignee(task.getAssignee());
            actTaskDTOList.add(actTaskDTO);
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        PageInfo<ActTaskDTO> actTaskDTOPageInfo = new PageInfo<>(actTaskDTOList);
        return new ResponseData<>().ok(actTaskDTOPageInfo);
    }

    @Override
    public ResponseData<?> getTaskHistoryListById(String id) {
        HistoricTaskInstanceQuery query = historyService.createHistoricTaskInstanceQuery();

        // 设置查询条件：根据ID查询
        query.taskId(id);

        // 执行查询
        HistoricTaskInstance historicTaskInstance = query.singleResult();
        ActRuTask actRuTask = new ActRuTask();
        BeanUtils.copyProperties(historicTaskInstance,actRuTask);
        return new ResponseData<>().ok(actRuTask);

    }

    @Override
    public ActRuTaskDto getTaskListByName(String username) {
        TaskQuery taskQuery = taskService.createTaskQuery()
                .taskAssignee(username);
        Task task = taskQuery.singleResult();
        ActRuTaskDto actRuTaskDto = new ActRuTaskDto();
        BeanUtils.copyProperties(actRuTaskDto,task);
        return actRuTaskDto;
    }
}




